package com.sz.stock.commons;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientUtil {

    public static String getMethod(String url){
        String result="";
        HttpClient client= HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            result= EntityUtils.toString(entity,"UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            get.releaseConnection();
        }
        return result;
    }

    public static String postMethod(String url,String json){
        String result="";
        HttpClient client= HttpClients.createDefault();
        HttpPost post = new HttpPost(url); //相当于表单提交
        try {
            StringEntity param = new StringEntity(json, "UTF-8");
            post.setHeader("Content-Type","application/json;charset=UTF-8");
            post.setEntity(param);
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            result= EntityUtils.toString(entity,"UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            post.releaseConnection();
        }
        return result;
    }




















}
